package com.xdwanj.servlet;

import com.mysql.jdbc.StringUtils;
import com.xdwanj.pojo.Commodity;
import com.xdwanj.pojo.User;
import com.xdwanj.service.CommodityService;
import com.xdwanj.service.ShoppingCartService;
import com.xdwanj.service.impl.CommodityServiceImpl;
import com.xdwanj.service.impl.ShoppingCartServiceImpl;
import com.xdwanj.util.ConstantsRes;
import com.xdwanj.util.ConstantsStr;
import com.xdwanj.util.UrlUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/commodity")
public class CommodityServlet extends HttpServlet {

    /**
     * 根请求地址
     */
    public static final String REQUEST_MAPPING = "commodity";

    /**
     * 方法属性名
     */
    public static final String METHOD = "method";

    /**
     * 查询动作
     */
    public static final String QUERY = "query";
    /**
     * 添加购物车
     */
    public static final String ADD_SHOPPING_CART = "addShoppingCart";


    /**
     * 商品ID
     */
    public static final String COID = "coid";
    /**
     * 商品数量
     */
    public static final String COUNT = "count";

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("进入CommodityServlet...");
        String method = req.getParameter(METHOD);

        switch (method) {
            case QUERY:
                query(req, resp);
                break;
            case ADD_SHOPPING_CART:
                addShoppingCart(req, resp);
                break;
            default:
                break;
        }
    }


    /**
     * 添加购物车
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private void addShoppingCart(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String url = null;
        boolean flag = false;

        ShoppingCartService shoppingCartService = new ShoppingCartServiceImpl();
        User user = (User) req.getSession().getAttribute(ConstantsStr.USER_SESSION);

        //获取UID
        int coid = 0;
        int count = 0;

        /*
          判断COID是否是严格的数字
         */
        if (StringUtils.isStrictlyNumeric(req.getParameter(COID))) {
            coid = Integer.parseInt(req.getParameter(COID));
        }

        /*
          判断COUNT是否是严格的数字
         */
        if (StringUtils.isStrictlyNumeric(req.getParameter(COUNT))) {
            count = Integer.parseInt(req.getParameter(COUNT));
        }


        flag = shoppingCartService.add(user.getId(), coid, count);

        if (flag) {
            req.setAttribute(ConstantsStr.STATUS, "添加成功!");
        } else {
            req.setAttribute(ConstantsStr.STATUS, "添加失败!");
        }

        url = UrlUtil.getUrl(PageServlet.REQUEST_MAPPING, new String[]{
                PageServlet.POSITION, PageServlet.SHOP,
                PageServlet.METHOD, ShopServlet.RECOMMEND});
        System.out.println("url = " + url);
        req.getRequestDispatcher(url).forward(req, resp);
    }


    /**
     * 查询Banner对象集合
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private void query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String url = null;
        String coidStr = req.getParameter(PageServlet.COID);
        CommodityService service = new CommodityServiceImpl();
        int coid = 0;

        if (StringUtils.isStrictlyNumeric(coidStr)) {
            coid = Integer.parseInt(coidStr);
        }

        Commodity commodity = service.findCommodityById(coid);

        req.setAttribute(ConstantsStr.COMMODITY, commodity);

        System.out.println("commodity = " + commodity);

        url = ConstantsRes.COMMODITY_PAGE;
        System.out.println("url = " + url);
        req.getRequestDispatcher(url).forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

}
